package com.kancy.stream.rabbit.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kancy.stream.rabbit.exception.MessageException;
import com.kancy.stream.rabbit.message.ChannelMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * LogMessageHandler
 *
 * @author kancy
 * @date 2020/3/8 11:54
 */
public class LogMessageHandler implements MessageHandler {
    private static Logger log = LoggerFactory.getLogger(LogMessageHandler.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 监听消息
     *
     * @param message
     * @throws Exception
     */
    @Override
    public void onMessage(ChannelMessage message) {
        try {
            String messageJsonString = objectMapper.writeValueAsString(message);
            log.info("消费消息成功：{}", messageJsonString);
        } catch (JsonProcessingException e) {
            throw new MessageException(e);
        }
    }
}
